package com.hjm.app.fragment.order;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.hjm.app.R;
import com.hjm.java.entity.Address;
import com.hjm.ui.order.OrderItem;
import com.hjm.ui.order.entity.Order;

import java.util.ArrayList;
import java.util.List;

public class FragmentOrderCreate extends Fragment {
    private TextView addressTextView;
    private Button cancelButton, payButton;
    private Address address;
    private String addressStr;
    private List<Order> orderList;
    private OrderItem orderItem;
    private String TAG = "FragmentOrderCreate";
    private OrderCallBackInterface orderCallBackInterface;

    public void setOrderCallBackInterface(OrderCallBackInterface orderCallBackInterface) {
        this.orderCallBackInterface = orderCallBackInterface;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            List<Order> orderList = getArguments().getParcelableArrayList("orderList");
            if (orderList != null){
                this.orderList = orderList;
            } else {
                orderList = new ArrayList<>();
            }
        }
    }

    public static FragmentOrderCreate newInstance(List<Order> orderList) {
        FragmentOrderCreate fragmentOrderDetail = new FragmentOrderCreate();
        Bundle args = new Bundle();
        args.putParcelableArrayList("orderList", (ArrayList<? extends Parcelable>) orderList);
        fragmentOrderDetail.setArguments(args);
        return fragmentOrderDetail;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_order_create, container, false);

        addressTextView = view.findViewById(R.id.text_view_create_order_address);
        if (address != null)
            addressTextView.setText(addressStr);

        orderItem = view.findViewById(R.id.order_item_create_order);
        setOrderItemData();

        cancelButton = view.findViewById(R.id.button_create_order_cancel);
        cancelButton.setOnClickListener((v)->{
            if (orderCallBackInterface != null && orderList!= null)
                orderCallBackInterface.onButtonClickListener("取消支付",orderList);
        });

        payButton = view.findViewById(R.id.button_create_order_pay);
        payButton.setOnClickListener((v)->{
            if (orderCallBackInterface != null && orderList!= null)
                orderCallBackInterface.onButtonClickListener("支付",orderList);
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void setAddress(Address address){
        this.address = address;
        addressStr = "收件人姓名："+address.getName()+
                "\n收件人电话："+address.getPhone()+
                "\n收件人地址："+address.getAddress();
        if (addressTextView != null)
            addressTextView.setText(addressStr);
    }

    public void setOrderList(List<Order> orders) {
        Log.d(TAG, "setOrderList: 订单条数 "+orders.size());
        orderList = orders;
        setOrderItemData();
    }

    void setOrderItemData(){
        if (orderList != null && orderItem != null){
            Log.d(TAG, "orderList 不为空且未设置数据");
            orderItem.setOrderList(orderList);
        } else {
            Log.d(TAG, "orderList 或 orderItem 为空");
        }
    }
}